/******************************************************************************
     Copyright (C) 2008  TANGUY Arnaud arn.tanguy@gmail.com
*                                                                             *
* This program is free software; you can redistribute it and/or modify        *
* it under the terms of the GNU General Public License as published by        *
* the Free Software Foundation; either version 2 of the License, or           *
* (at your option) any later version.                                         *
*                                                                             *
* This program is distributed in the hope that it will be useful,             *
* but WITHOUT ANY WARRANTY; without even the implied warranty of              *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License along     *
* with this program; if not, write to the Free Software Foundation, Inc.,     *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 *
*																			  *
* --------------------------------------------------------------------------- *
*																		      *
*	Modifications for FotoKinect, Spring 2011 by Gianna Badiali,		      *
*                                                   Gabriel Fernandez,	      *
*                                                   Kelsey Tripp, and         *
*                                                   Sam Swarr                 *
*																			  *
*******************************************************************************/

#include "CroppingWidget.h"
#include "CPixmap.h"
#include <QRubberBand>
#include <QMouseEvent>
#include <QPainter>


// creates the cropping widget with no pixmap
CroppingWidget::CroppingWidget(QWidget *parent) : QWidget(parent, Qt::WindowFlags(Qt::FramelessWindowHint)), m_previewRatio(1)
{
    m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    m_rubberBand->setGeometry(0,0,0,0);
    m_rubberBand->show();
}


// sets the pixmap of the cropping widget, scaled to a constant ratio, and resizes the widget appropriately
void CroppingWidget::setPixmap(CPixmap *pix)
{
    m_photo = pix;
    m_previewPixmap = m_photo->scaled(600, 450, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    m_previewRatio = (float)m_photo->width()/(float)m_previewPixmap.width();
    setFixedSize(m_previewPixmap.size());
}


// updates the rubberband graphically to be the given rect
void CroppingWidget::updateRubberBand(QRect cropSelection) {
    m_rubberBand->setGeometry(cropSelection);
	m_rubberBand->show();
}


// paints the pixmap onto the widget
void CroppingWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.drawPixmap(m_previewPixmap.rect(), m_previewPixmap);
}


// returns the cropping rect (rubberband)
QRect CroppingWidget::getCroppingRect() const
{
    QRectF selectionRect = m_rubberBand->geometry();
    return QRect(selectionRect.x()*m_previewRatio, selectionRect.y()*m_previewRatio,
                 selectionRect.width()*m_previewRatio, selectionRect.height()*m_previewRatio);
}
